package com.synv.util;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.synv.entity.Category;
import com.synv.entity.FlashCard;
import com.synv.flashcard.ContentActivity;
import com.synv.flashcard.R;
import com.synv.flashcard.WordDetailActivity;

public class WordListAdapter  extends ArrayAdapter<FlashCard>{

	Context context; 
    int layoutResourceId;
    List<FlashCard> flashcards;
    int categorySeq;
    
    public WordListAdapter(Context context, int layoutResourceId, List<FlashCard> flashcards) {
		super(context, layoutResourceId, flashcards);

		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.flashcards = flashcards;
	}
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WordListHolder holder = null;
        
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            
            holder = new WordListHolder();
            holder.bookmark = (CheckBox)row.findViewById(R.id.bookmark);
            holder.txtWordTitle = (TextView)row.findViewById(R.id.txtWordTitle);
            holder.btnDeleteWord =(Button) row.findViewById(R.id.btnDeleteWord);
            holder.rlDeleteWord = (RelativeLayout) row.findViewById(R.id.rlDeleteWord);
            row.setTag(holder);
        }
        else
        {
            holder = (WordListHolder)row.getTag();
        }
        
        FlashCard flashcard = flashcards.get(position);
        holder.txtWordTitle.setText(flashcard.getWord());
        holder.bookmark.setChecked(flashcard.getBookmark() > 0 ? true : false);
        
        holder.bookmark.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				CheckBox checkbox = (CheckBox) arg0;
				if(categorySeq == Category.MARKED_CATEGORY) {
					checkbox.setChecked(true);
					return;
				}
				FlashCard flashcard = flashcards.get(position);
				bookmark(flashcard);
			}
        	
        });
        
        holder.btnDeleteWord.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				FlashCard flashcard = flashcards.get(position);
				delete(flashcard, categorySeq);
			}
		});
        
        /*holder.txtWordTitle.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent contentScreen = new Intent(context, WordDetailActivity.class);
				TextView view = (TextView)arg0;
				contentScreen.putExtra("word", view.getText().toString());
				context.startActivity(contentScreen);
			}
		});*/
        return row;
    }
    
    private void delete(FlashCard flashcard, Integer categorySeq) {
    	DataBaseHelper myDbHelper = new DataBaseHelper(context);
    	if(categorySeq == Category.MARKED_CATEGORY) {
    		myDbHelper.getWritableDatabase().execSQL("UPDATE flashcard SET bookmark = 0 WHERE seq = " + flashcard.getSeq());
    	} else {
    		myDbHelper.getWritableDatabase().execSQL("UPDATE flashcard SET category = REPLACE(category, '," + categorySeq +",', ',') WHERE seq = " + flashcard.getSeq());
    	}
    	myDbHelper.close();
    	this.remove(flashcard);
    	this.notifyDataSetChanged();
    }
    
    private void onclick(FlashCard flashcard) {
    	
		Intent contentScreen = new Intent(context, WordDetailActivity.class);
		context.startActivity(contentScreen);
    }
    
    private void bookmark(FlashCard flashcard) {
    	/*if(category.getBookmark() != Category.ALL_MARK) {
    		category.setBookmark(Category.ALL_MARK);
    	} else if (category.getBookmark() == Category.ALL_MARK) {
    		category.setBookmark(Category.NOT_MARK);
    	}
    	try {
	    	DataBaseHelper myDbHelper = new DataBaseHelper(context);
	    	myDbHelper.getWritableDatabase().execSQL("UPDATE flashcard SET Bookmark = " + category.getBookmark() +" WHERE Category LIKE '%," + category.getSeq() + ",%'");
	    	myDbHelper.getWritableDatabase().execSQL("UPDATE category SET IsBookmark = " + category.getBookmark() + " WHERE Seq = " + category.getSeq());
	    	myDbHelper.close();
	    	Toast.makeText(context, "Bookmark successfully!", Toast.LENGTH_SHORT).show();
    	} catch (Exception ex) {
    		Toast.makeText(context, "Bookmark failure!", Toast.LENGTH_SHORT).show();
    	}*/
    }
    
    @Override
    public FlashCard getItem(int position) {
    	return flashcards.get(position);
    }
    
    static class WordListHolder
    {
        TextView txtWordTitle;
        CheckBox bookmark;
        Button btnDeleteWord;
        RelativeLayout rlDeleteWord;
    }
    
    public void setCategorySeq(Integer categorySeq) {
    	this.categorySeq = categorySeq;
    }
}
